package com.example.sqlitetest.View.widget;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.sqlitetest.Model.SQLiteDao;
import com.example.sqlitetest.R;

public class NoteEditActivity extends Activity {

	private Button  mComperBtn, mCancelBtn;
	private EditText mContentWrite;
	private SQLiteDao  mDao;
	private Cursor  mCursor;
	// 声明字段id
	private int _id = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_note_edit);
		initViews();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initViews() {
        mDao = new SQLiteDao(this);
		// 得到游标
		mCursor = mDao.select();
        mComperBtn = (Button)findViewById(R.id.btn_ok);
		mCancelBtn = (Button)findViewById(R.id.btn_cancle);
		mContentWrite = (EditText) findViewById(R.id.et_content);

		mComperBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
                addData();
				Intent intent = new Intent(NoteEditActivity.this, NoteActivity.class);
				startActivity(intent);
				finish();
			}
		});

		mCancelBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(NoteEditActivity.this, NoteActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	private void addData() {
		if (mContentWrite.getText().toString().equals("")){
			Toast.makeText(NoteEditActivity.this, "文本框不能为空", Toast.LENGTH_SHORT).show();
		}else{
            mDao.insert(mContentWrite.getText().toString()); // 插入数据
            mCursor.requery(); // 刷新游标
			mContentWrite.setText(""); // 赋值
			_id = 0; // 清零
		}
	}
}
